package class01;

public class MyCode02_SumOfFactorial {

    //阶乘函数
    public static long factorial(int N) {
        long ans = 1;
        for (int i = 1; i <= N; i++) {
            ans *= i;
        }
        return ans;
    }

    //1 - N 的阶乘累加
    //1！+2！+....+N!
    //每个数的阶乘独立计算
    public static long f1(int N) {
        long ans = 0 ;
        for (int i = 1; i <= N; i++) {
            ans += factorial(i);
        }
        return ans;
    }

    //1 - N 的阶乘累加
    //1！+2！+....+N!
    //优化
    public static long f2(int N) {
        long ans = 0 ;
        long cur = 1 ;
        for (int i = 1; i <= N; i++) {
            cur *= i ;
            ans += cur;
        }
        return ans;
    }

    public static void main(String[] args) {
        int N = 100000;
        long start = System.currentTimeMillis();
        System.out.println(f1(N));
        System.out.println(System.currentTimeMillis()-start);

        start = System.currentTimeMillis();
        System.out.println(f2(N));
        System.out.println(System.currentTimeMillis()-start);
    }
}
